<template>
	<view class="search">
		<musichead title="搜索" :icon="true" :iconblack='true'></musichead>
		<view class="container">
			<scroll-view scroll-y="true">
				<view class="search-search">
					<text class="iconfont icon-Magnifier"></text>
					<input @input="handtoSuggest" @confirm="headToSearch(searchWord)" type="text" placeholder="搜索歌曲" v-model="searchWord"/>
					<text @tap="handleToClose()" v-show="searchType !=1" class="iconfont icon-guanbi"></text>
				</view>
				<block v-if="searchType == 1">
				
				<view class="search-history">
					<view class="search-history-head">
						<text>历史记录</text>
						<text @tap="headToClear()" class="iconfont icon-lajitong"></text>
					</view>
					<view class="search-history-list">
						<view @tap="headleToWord(item)" v-for="(item,index) in searchHistory" :key='index'>{{item}}</view>
						
					</view>
				</view>
				
				
				<view class="search-hot">
						<view class="search-hot-title">热搜榜</view>
						<view @tap="headleToWord(item.searchWord)" class="search-hot-item" v-for="(item,index) in searchHot" :key='index'>
							<view class="search-hot-top">{{index+1}}</view>
							<view class="search-hot-word">
								<view>
									{{item.searchWord}}
									<image :src="item.iconUrl" mode="aspectFit"></image>
								</view>
								<view>{{item.content}}</view>
							</view>
							<text class="search-hot-count">{{item.score}}</text>
						</view>
				</view>
				</block>
				<block v-else-if="searchType ==2">
					<view class="search-result">
						<view v-for="(item,index) in searchlist" :key='index' class="search-result-item"
						 @tap="handleToDetali(item.id)">
							<view class="search-result-word">
								<view>{{item.name}}</view>
								<view>{{item.artists[0].name}} - {{item.album.name}}</view>
							</view>
							<text class="iconfont icon-bofang"></text>
						</view>
					</view>
				</block>
				<block v-else-if="searchType ==3">
					<view class="search-suggest">
						<view class="search-suggest-head">搜索“{{searchWord}}”</view>
						<view @tap="headleToWord(item.keyword)" class="search-suggest-item" v-for="(item,index) in searchSuggest" :key='index'>
							<text class="iconfont icon-bofang"></text>
							{{item.keyword}}
						</view>
					</view>
				</block>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	// 引入返回上一级、返回首页 头部组件
	import musichead from '../../components/musichead/musichead.vue'
	// 引入css绝对路径
	import '../../common/iconfont.css'
	import {searchHot, searchWord, searchSuggest} from '../../common/api.js'
	export default {
		data() {
			return {
				searchHot:[],
				searchWord:'',
				searchHistory:[],
				searchType:1,
				searchlist:[],
				searchSuggest:[]
			}
		},
		onLoad() {
			searchHot().then((res)=>{
			if(res[1].data.code == '200'){
				this.searchHot = res[1].data.data;
			}	
			})
			uni.getStorage({
				key:'searchHistory',
				success:(res)=>{
					this.searchHistory = res.data
				}
			})
		},
		components:{
			musichead
		},
		methods: {
			headleToWord(word){
				this.searchWord = word;
				this.headToSearch(word)
			},
			headToSearch(word){
				this.searchHistory.unshift(word);
				this.searchHistory = [...new Set(this.searchHistory)];
				// 搜索记录内容不超过 10 个历史记录
				if(this.searchHistory.length > 10){
					this.searchHistory.length = 10;
				}
				// 下次刷新的时候，还能看到搜索记录
				uni.setStorage({
				    key: 'searchHistory',
				    data: this.searchHistory
				});
				this.getSearchList(word);
			},
			headToClear(){
				uni.clearStorage({
					key : 'searchHistory',
					success:(res)=>{
						this.searchHistory= []
					}
				});
				
			},
			getSearchList(word){
				searchWord(word).then((res)=>{
					if(res[1].data.code =='200'){
						this.searchlist = res[1].data.result.songs;
						this.searchType = 2
					}
				})
			},
			handleToClose(){
				this.searchWord = '';
				this.searchType =1;
			},
			handleToDetali(songId){
				uni.navigateTo({
					url: '/pages/detail/detail?songId='+ songId
					
				});
			},
			handtoSuggest(ev){
				let value=  ev.detail.value;
				if(!value){
					this.searchType = 1;
					return;
				}
				searchSuggest(value).then((res)=>{
					if(res[1].data.code =='200'){
					this.searchSuggest = res[1].data.result.allMatch;
					this.searchType = 3;
					}
				})
				// console.log(value)
			},
		}
	}
</script>

<style scoped>
.search-search{
	display: flex;
	align-items: center;
	height:70rpx;
	margin: 70rpx 30rpx 30rpx 30rpx;
	background-color: #f7f7f7;
	border-radius: 50rpx;
}
.search-search text{
	margin: 0 26rpx;
}
.search-search input{
	flex: 1;
	font-size: 26rpx
}
.search-history{
	margin: 0 30rpx;
	font-size: 26rpx
}
.search-history-head{
	display: flex;
	justify-content: space-between;
	margin-bottom: 36rpx;
}
.search-history-list{
	display: flex;
	flex-wrap: wrap;
}
.search-history-list view{
	padding: 16rpx 28rpx;
	border-radius: 40rpx;
	margin-right: 30rpx;
	margin-bottom: 30rpx;
	background-color: #f7f7f7;
}
.search-hot{
	margin: 30rpx 30rpx;
	font-size: 26rpx;
	color: #bebebe;
}
.search-hot-item{
	display: flex;
	align-items: center;
	margin-top: 40rpx;
}
.search-hot-top{
	color: #fb2222;
	width: 60rpx;
	margin-left: 8rpx;
	font-size: 34rpx;
}
.search-hot-word{
	flex: 1;
}
.search-hot-word view:nth-child(1){
	font-size: 32rpx;
	color: black;
}
.search-hot-word image{
	width:48rpx;
	height: 22rpx;
}
.search-hot-count{

}
.search-result{
	border-top: 2px solid #e4e4e4;
	padding: 30rpx;
}
.search-result-item{
	display: flex;
	justify-content: space-between;
	align-items: center;
	padding-bottom: 30rpx;
	margin-bottom: 30rpx;
	border-bottom: 2px solid #e4e4e4;
}
.search-result-item text{
	font-size: 50rpx;
}
.search-result-word{}
.search-result-word view:nth-child(1){
	font-size: 28rpx;
	color: #235790;
	margin-bottom: 12rpx;
}
.search-result-word view:nth-child(2){
	font-size: 22rpx;
	color: #898989;
}
.search-suggest{
	border-top: 2px solid #e4e4e4;
	padding: 30rpx;
	font-size: 26rpx;
}
.search-suggest-head{
	color: #4574a5;
	margin-bottom: 74rpx;
}
.search-suggest-item{
	color: #5d5d5d;
	margin-bottom: 74rpx;
}
.search-suggest-item text{
	color: #bdbdbd;
	margin-right: 28rpx;
	position: relative;
	top: 2rpx;
	
}
</style>
